var app = getApp();

Page({
  data: {
    Whospital: "",
    imgFlag: [],
    addressBoo: !0,
    provinceBoo: !0,
    type: 0,
    text: "",
    city: "",
    intro: "",
    cityData: [],
    region: [],
    tapFlag: !0,
    height: 500,
    textFlag: !0,
    typeHeight: 400,
    typeindex: 0,
    is_index: 0,
    jigou_onearr: [],
    jigou_oneindex: 0,
    jigou_twoarr: [],
    jigou_twoindex: 0,
    keshi_onearr: [],
    keshi_oneindex: 0,
    keshi_twoindex: 0,
    keshi_twoarr: [],
    ctname: '',
    thumb: "",
    imgpath: "",
    typeArray: ['线上团队', '线下团队'],
    sexArray: ["线上科室", "自由团体"],
    shequArray: [],
    shequindex: 0,
    sexIndex: "",
    data_arr: [],
    lt_array: ['否', '是'],
    show_array: ['否', '是'],
    lt_index: 0,
    pz_array: ['否', '是'],
    pz_index: 0,
    jj_array: ['否', '是'],
    jj_index: 0,
    picc_array: ['否', '是'],
    picc_index: 0,
    is_green: "",
    is_examine: "",
    is_urgent: "",
    is_index: 0,
    type: 0,
    Gender: ['女', '男'],
    Hospital: ['大医院', '省第三人民医院'],
    hosindex: '',
    Department: ['科室1', '科室2'],
    deparindex: '',
    docTitle: ['职称1', '职称2'],
    docindex: '',
    belist: [{
      'name': '青光眼',
      'checked': false
    },
    {
      'name': '青光眼',
      'checked': false
    },
    {
      'name': '青光眼',
      'checked': false
    },
    ],
    id: "",
    fenleiList: [{
      id: 1,
      name: '医院'
    },
    {
      id: 2,
      name: '药房'
    },
    {
      id: 3,
      name: '体检机构'
    },
    {
      id: 4,
      name: '诊所'
    }
    ]
  },
  bindpiccChange: function (e) {
    var index = e.detail.value;
    var picc_array = this.data.picc_array;
    this.setData({
      picc_index: index,
      is_picc: index
    })
  },
  bindjjChange: function (e) {
    var index = e.detail.value;
    this.setData({
      jj_index: index,
      is_urgent: index
    })
  },
  bindpzChange: function (e) {
    var index = e.detail.value;
    this.setData({
      pz_index: index,
      is_examine: index
    })
  },
  bindlvtongChange: function (e) {
    var index = e.detail.value;
    this.setData({
      lt_index: index,
      is_green: index
    })
  },
  radioChange: function (e) {
    var index = e.detail.value;
    this.setData({
      is_index: index,
    })
  },
  chosefenlei: function (e) {
    this.setData({
      fenleiIndex: e.detail.value,
      fqname: this.data.fenleiList[e.detail.value].name,
      qx_id: this.data.fenleiList[e.detail.value].id,
    })
  },
  // 选择所在地
  bindRegionChange: function (e) {
    this.setData({
      cityData: e.detail.value,
      isindex: 0
    })
  },
  clicks: function (e) {
    let index = e.currentTarget.dataset.index;
    let arrs = this.data.label_arr;

    if (arrs[index].checked == false) {
      arrs[index].checked = true;
    } else {
      arrs[index].checked = false;
    }
    this.setData({
      label_arr: arrs
    })
    // console.log(e)
  },
  bindPickerChange: function (e) {
    var index = e.detail.value;
    var Gender = this.data.Gender
    console.log(e);
    console.log(Gender[index])
    this.setData({
      z_sex: index
    })
  },
  bindhosindexChange: function (e) {
    this.setData({
      deparindex: e.detail.value
    })
  },
  changetitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  changetelphone: function (e) {
    this.setData({
      telphone: e.detail.value
    })
  },
  binddocTitle: function (e) {
    this.setData({
      docindex: e.detail.value
    })
  },
  formSubmit: function (e) {

    var values = {};
    var that = this;
    values.title = that.data.title;
    values.telphone = that.data.telphone;
    values.type = that.data.type;
    values.is_index = that.data.is_index;
    values.content = that.data.content;
    values.thumb = that.data.thumb;
    values.imgpath = that.data.imgpath;
    values.cid = that.data.cid;
    var cityData = that.data.cityData;

    values.address = that.data.address;
    values.province = that.data.cityData[0];
    values.city = that.data.cityData[1];
    values.district = that.data.cityData[2];
    values.lon = that.data.lng;
    values.lat = that.data.lat;
    values.openid = wx.getStorageSync('openid');
    values.zid = wx.getStorageSync('zid');
    values.id = that.data.id;
    values.jigou_one = that.data.qx_id;
    values.jigou_two = that.data.jigou_two;
    values.keshi_one = that.data.keshi_one;
    values.keshi_two = that.data.keshi_two;
    values.label = that.data.authority;
    var str = /^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/
    if (!values.thumb) {
      wx.showToast({
        title: '请上传团队LOGO',
        icon: 'none'
      })
    } else if (!values.imgpath) {
      wx.showToast({
        title: '请上传荣誉资质',
        icon: 'none'
      })
    } else if (!values.title) {
      wx.showToast({
        title: '请填写团队名称',
        icon: 'none'
      })
    } else if (!values.telphone) {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none'
      })
    } else if (!str.test(values.telphone)) {
      wx.showToast({
        title: '请正确填写联系电话',
        icon: 'none'
      })
    } else
      if (values.type === '') {
        wx.showToast({
          title: '请选择类别',
          icon: 'none'
        })
      } else if (!values.province) {
        wx.showToast({
          title: '请选择所在地',
          icon: 'none'
        })
      } else if (values.is_index === '') {
        wx.showToast({
          title: '请选择是否显示',
          icon: 'none'
        })
      } else if (values.jigou_one === '') {
        wx.showToast({
          title: '请选择一级机构',
          icon: 'none'
        })
      } else if (values.jigou_two == '') {
        wx.showToast({
          title: '请选择二级机构',
          icon: 'none'
        })
      } else if (!values.keshi_one) {
        wx.showToast({
          title: '请选择一级科室',
          icon: 'none'
        })
      } else if (!values.keshi_two) {
        wx.showToast({
          title: '请选择二级科室',
          icon: 'none'
        })
      } else if (!values.content) {
        wx.showToast({
          title: '请输入简介',
          icon: 'none'
        })
      } else {
        app.util.request({
          'url': "entry/wxapp/team.add_team",
          data: values,
          success: function (res) {
            wx.showToast({
              title: '申请成功，请等待审核',
              icon: 'none'
            })
            setTimeout(function () {
              wx.navigateBack({})
            }, 3000)
          }
        })
      }
  },
  bindSexChange: function (a) {
    console.log(a), this.setData({
      sexIndex: a.detail.value
    }), 0 == a.detail.value && (this.data.teamType = 1), 1 == a.detail.value && (this.data.teamType = 2);
  },
  chooseImage: function () {
    var that = this;
    var uniacid = that.data.uniacid
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        console.log(tempFilePaths);
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=upload&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function (res) {
            console.log(res.data)
            that.setData({
              uplogo: res.data
            })
          },
          fail: function (res) {
            console.log(res)
          },
        })
        that.setData({
          logo: tempFilePaths
        })
      }
    })
  },

  getText: function (a) {
    this.setData({
      text: a.detail.value
    });
  },
  getIntro: function (a) {
    this.setData({
      intro: a.detail.value
    });
  },
  bindlinechange: function (a) {
    1 != this.data.type ? a.detail.heightRpx > 500 && this.setData({
      height: a.detail.heightRpx
    }) : a.detail.heightRpx > 400 && this.setData({
      typeHeight: a.detail.heightRpx
    });
  },
  getCity: function (a) { },
  save: function () {
    var that = this
    var teamname = that.data.text
    var teamaddress = that.data.address
    var teamtext = that.data.intro
    var teamtype = that.data.sexIndex
    var teampic = that.data.teampic1
    var t_id = that.data.t_id
    app.util.request({
      'url': 'entry/wxapp/studio.docteam',
      data: {
        teamname: teamname,
        teamaddress: teamaddress,
        teamtext: teamtext,
        teamtype: teamtype,
        teampic: teampic,
        zid: that.data.zid,
        t_id: t_id
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: "保存成功",
          icon: "none",
          duration: 2e3,
          success: function () {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        })
      }
    })
  },
  creat: function () {
    var that = this
    var teamname = that.data.text
    var teamaddress = that.data.address
    var teamtext = that.data.intro
    var teamtype = that.data.sexIndex

    if (teamname == '') {
      wx.showToast({
        title: "请填写团队名称",
        icon: "none",
        duration: 2e3
      })
    } else if (teamaddress == undefined) {
      wx.showToast({
        title: "请选择团队所在地区",
        icon: "none",
        duration: 2e3
      })
    } else if (teamtype == undefined) {
      wx.showToast({
        title: "请选择团队类型",
        icon: "none",
        duration: 2e3
      })
    } else if (teamtext == '') {
      wx.showToast({
        title: "请填写团队简介",
        icon: "none",
        duration: 2e3
      })
    } else {
      var teampic = that.data.uplogo
      app.util.request({
        'url': 'entry/wxapp/studio.docteam',
        data: {
          teamname: teamname,
          teamaddress: teamaddress,
          teamtext: teamtext,
          teamtype: teamtype,
          teampic: teampic,
          zid: that.data.zid
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: "创建成功",
            icon: "none",
            duration: 2e3,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }
          })
        }
      })

    }

  },
  onLoad: function (t) {
    var that = this;
    var id = t.id;
    var uniacid = app.siteInfo.uniacid;
    if (t.id != undefined) {
      app.util.request({
        'url': 'entry/wxapp/team.team_detail',
        data: {
          id: id,
        },
        success: function (res) {
          var cityData =[];
          cityData[0] = res.data.province;
          cityData[1] = res.data.city;
          cityData[2] = res.data.district;
          that.setData({
            thumb: res.data.thumb,
            imgpath: res.data.imgpath,
            info: res.data,
            id: t.id,
            title : res.data.title,
            telphone : res.data.telphone,
            type : res.data.type,
            is_index : res.data.is_index,
            content : res.data.content,
            thumb : res.data.thumb,
            imgpath : res.data.imgpath,
            cid : res.data.cid,
            cityData : cityData,
            address : res.data.address,
            province : res.data.province,
            city : res.data.city,
            district : res.data.district,
            lon : res.data.lng,
            lat : res.data.lat,
            jigou_one : res.data.qx_id,
            jigou_two : res.data.jigou_two,
            keshi_one : res.data.keshi_one,
            keshi_two : res.data.keshi_two,
            label : res.data.label,
            authority:res.data.label
          })
        },
      })
    }
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      uniacid: uniacid,

    })
    // 获取社区列表
    that.getshequ();
    // 获取一级科室列表
    that.getKeshi_one();
    // 获取机构一级
    that.getHospital()
    that.getjigou_two(1);
  },
  changeinput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 获取社区列表
  getshequ: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.shequ",
      success: function (res) {
        var list = [];
        for (var i in res.data) {
          list.push(res.data[i].title)
        }
        that.setData({
          shequArray: list,
          shequArrays: res.data
        })
      }
    })
  },
  input:function(e){
    this.setData({
      authority: e.detail.value
    })
  },
  // 获取一级科室列表
  getKeshi_one: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.keshi_one",
      success: function (res) {
        console.log(res)
        var list = [];
        for (var i in res.data) {
          list.push(res.data[i].ctname)
        }
        that.setData({
          keshi_onearr: list,
          keshi_onearrs: res.data,
          keshi_one: res.data[0]['id']
        })
        that.getKeshi_two(res.data[0].id)
      }
    })
  },
  // 获取科室二级
  getKeshi_two: function (id) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.keshi_two",
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        var list = [];
        for (var i in res.data) {
          list.push(res.data[i].name)
        }
        that.setData({
          keshi_twoarr: list,
          keshi_twoarrs: res.data,
          keshi_two: res.data[0].id
        })

      }
    })
  },
  // 获取机构一级
  getHospital: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.hospital",
      success: function (res) {
        var list = [];
        for (var i in res.data) {
          list.push(res.data[i].name)
        }
        that.setData({
          jigou_onearr: list,
          jigou_onearrs: res.data,
        })
      }
    })
  },
  getjigou_two: function (id) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.jigou_two",
      data: {
        id: id
      },
      success: function (res) {
        var list = [];
        for (var i in res.data) {
          list.push(res.data[i].agentname)
        }
        that.setData({
          jigou_twoarr: list,
          jigou_twoarrs: res.data
        })
      }
    })
  },
  // 团队类型修改
  bindtypeChange: function (e) {
    var typeindex = e.detail.value
    this.setData({
      typeindex: e.detail.value,
      type: e.detail.value,
      teamtype: this.data.typeArray[typeindex]
    })
  },
  // 所属社区修改
  bindshequChange: function (e) {
    var shequArray = this.data.shequArrays;
    var index = e.detail.value;
    var cid = shequArray[index].id
    this.setData({
      shequindex: e.detail.value,
      cid: cid
    })
  },
  // 一级机构修改
  bindjgoneChange: function (e) {
    console.log(e)
    var that = this
    var fenleiList = this.data.fenleiList
    var qx_id = this.data.fenleiList[e.detail.value].id;
    var fenleiIndex = e.detail.value
    var jigou_onearr = this.data.jigou_onearr;
    var index = e.detail.value;
    var jigou_one = parseInt(index) + 1;
    this.setData({
      fenleiIndex: e.detail.value,
      qx_id: qx_id,
      jigou_one: jigou_one,
      jigou_oneindex: e.detail.value,
      fqname: fenleiList[fenleiIndex].name
    })
    this.getjigou_two(jigou_one)
  },
  // 二级机构修改
  bindjgtwoChange: function (e) {
    var index = e.detail.value;
    var jigou_twoarr = this.data.jigou_twoarrs;
    var jigou_two = jigou_twoarr[index].hid
    this.setData({
      jigou_twoindex: e.detail.value,
      jigou_two: jigou_two
    })
    console.log(e);
    console.log(jigou_two)
  },
  // 一级科室修改
  bindksoneChange: function (e) {
    var keshi_onearr = this.data.keshi_onearrs;
    var index = e.detail.value;
    var keshi_one = keshi_onearr[index].id;
    this.setData({
      keshi_oneindex: e.detail.value,
      keshi_one: keshi_one,
      ctname: keshi_onearr[index].ctname
    })
    this.getKeshi_two(keshi_one);
  },
  // 二级科室修改
  bindkstwoChange: function (e) {
    var keshi_twoarr = this.data.keshi_twoarrs;
    var index = e.detail.value;
    var keshi_two = keshi_twoarr[index].id
    this.setData({
      keshi_twoindex: e.detail.value,
      keshi_two: keshi_two,
      name: keshi_twoarr[index].name
    })
    this.getLabel(keshi_two);
  },
  // 上传logo
  chooseimg: function () {
    var that = this;
    var uniacid = that.data.uniacid;
    console.log(that.data.url);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              that.setData({
                thumb: res.data,
              })
            }
          })
        }
      }
    })
  },
  chooseimgpath: function () {
    var that = this;
    var uniacid = that.data.uniacid;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              that.setData({
                imgpath: res.data,
              })
            }
          })
        }
      }
    })
  },
  // 获取标签
  getLabel: function (id) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.labels",
      data: {
        id: id
      },
      success: function (res) {

        that.setData({
          label_arr: res.data
        })
      }
    })
  },
  onReady: function () { },
  onShow: function () {
    var a = wx.createAnimation({
      duration: 800,
      timingFunction: "ease"
    });
    this.animation = a;
    var that = this
    let pages = getCurrentPages();
    console.log(that.data);
    let currPage = pages[pages.length - 1];
    console.log(currPage.data.agentname)
    if (currPage.data.hid && currPage.data.agentname) {
      that.setData({ //将携带的参数赋值
        hid: currPage.data.hid,
        agentname: currPage.data.agentname
      })
    }
  },
  //用户选择收货地址
  chooseLocation: function () {
    var that = this;
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function (res) {
          console.log(JSON.stringify(res));

          var address = res.provinceName + res.cityName + res.countyName + res.detailInfo
          console.log(res);
          that.setData({
            address: address,
            //具体收货地址显示
            flag: false,
          })
        },
        fail: function (err) {
          console.log(JSON.stringify(err));
          console.info("收货地址授权失败");

        }
      })
    } else {
      console.log('当前微信版本不支持chooseAddress');
    }
  },
  tapProvince: function (t) {
    this.setData({
      cityList: ""
    });
    var e = t.target.id,
      o = t.target.dataset.index,
      i = t.target.dataset.province,
      n = this;
    this.setData({
      provinceIndex: o,
      province: i,
      Seng: t.currentTarget.dataset.province
    }), n.data.provinceBoo && wx.request({
      url: a.globalData.httpOne + "mall/addr/listNation/" + e,
      success: function (a) {
        200 == a.data.code && n.setData({
          cityList: a.data.data,
          cityIndex: "none",
          districtIndex: "none",
          provinceBoo: !0,
          province: i
        });
      }
    }), n.setData({
      provinceBoo: !1
    });
  },
  tapCity: function (a) {
    this.setData({
      city: "",
      textFlag: !0
    });
    var t = a.target.dataset.index,
      e = a.target.dataset.city,
      o = this.data.province;
    this.animation.translateX(1200).step(), this.setData({
      code: a.target.dataset.code,
      cityIndex: t,
      city: e,
      area: o + "-" + e,
      addressBoo: !0,
      animationData: this.animation.export(),
      SHI: a.currentTarget.dataset.city
    });
  },
  closesearea: function () {
    this.setData({
      textFlag: !0
    }), this.animation.translateX(1200).step(), this.setData({
      addressBoo: !0,
      animationData: this.animation.export()
    });
  },
  onHide: function () { },
  shanchu1: function (t) {
    var e = this;
    wx.showModal({
      title: "提示",
      content: "您确认退出团队吗？",
      success: function (t) {
        t.confirm ? wx.request({
          url: a.globalData.dic + "doctor/team/quit/" + e.data.token + "/" + e.data.teamId,
          success: function (a) {
            if (200 == a.data.code) {
              if (1 == e.data.groupFlag) return void wx.navigateBack({
                delta: 4
              });
              wx.navigateBack({
                delta: 3
              });
            }
          }
        }) : t.cancel;
      }
    });
  },
  shanchu: function (t) {
    var that = this;
    var t_id = that.data.t_id
    wx.showModal({
      content: '是否解散团队',
      success: function (res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/seledocteam',
            data: {
              t_id: t_id,
              op: 'delete',
            },
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '解散成功',
                icon: "none",
                duration: 2e3,
                success: function () {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 2
                    })
                  }, 2000)
                }
              })
            },
          })
        }
      }
    })

  },
  Whospital: function (a) {
    var qx_id = this.data.qx_id;
    if (qx_id == '') {
      wx.showToast({
        title: '请先选择所属分组',
      })
    } else {
      this.setData({
        num: 0,
        numText: "保存"
      }), wx.setStorageSync("url", "doctor/info/update/"), wx.navigateTo({
        url: "/hyb_yl/backstageLifes/pages/WapproveWhospital/WapproveWhospital?qx_id=" + qx_id
      });
    }

  },
  mapViewTap() {
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',
      complete: function (res) {
        console.log(res)
        wx.chooseLocation({
          success(str) {
            console.log(str)
            var latitude = str.latitude
            var longitude = str.longitude

            var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
            //发送请求通过经纬度反查地址信息
            var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
            wx.request({
              url: getAddressUrl,
              success: function (result) {
                console.log(result)
                var province = result.data.result.address_component.province;
                var city = result.data.result.address_component.city;
                var district = result.data.result.address_component.district;
                var address = result.data.result.address_component.province + '-' + result.data.result.address_component.city + '-' + result.data.result.address_component.district;
                console.log(address)
                that.setData({
                  address: address,
                  lng: longitude,
                  lat: latitude,
                  province: province,
                  city: city,
                  district: district
                })
              }
            })
          }
        })
      }
    })
  },
});